package com.jojo.bubble.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.AlipayConfig;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;


public class AlipayFundTransUniTransfer {

    public static String aliFund(String orderNo, String name, String account, String price) throws AlipayApiException {
        String privateKey = AlipayTradeWapPay.privateKey;
        CertAlipayRequest alipayConfig = new CertAlipayRequest();
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(AlipayTradeWapPay.appId);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setEncryptor("");
        alipayConfig.setFormat("json");
        alipayConfig.setCertPath("/root/cert/appCertPublicKey_2021004104654022.crt");
        alipayConfig.setAlipayPublicCertPath("/root/cert/alipayCertPublicKey_RSA2.crt");
        alipayConfig.setRootCertPath("/root/cert/alipayRootCert.crt");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
        model.setOutBizNo(orderNo);
        model.setRemark("泡泡机提现");
        model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");
        model.setBizScene("DIRECT_TRANSFER");
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentity(account);
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        payeeInfo.setName(name);
        model.setPayeeInfo(payeeInfo);
        model.setTransAmount(price);
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        model.setOrderTitle("201905代发");
        request.setBizModel(model);
        AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
//        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
            return response.getCode();
        } else {
            System.out.println("调用失败");
            return response.getSubMsg();
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    public static void main(String[] args) throws AlipayApiException {
        String privateKey = AlipayTradeWapPay.privateKey;
        CertAlipayRequest alipayConfig = new CertAlipayRequest();
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(AlipayTradeWapPay.appId);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setEncryptor("");
        alipayConfig.setFormat("json");
        alipayConfig.setCertPath("D:\\jojo\\bubble\\src\\main\\resources\\cert\\appCertPublicKey_2021004104654022.crt");
        alipayConfig.setAlipayPublicCertPath("D:\\jojo\\bubble\\src\\main\\resources\\cert\\alipayCertPublicKey_RSA2.crt");
        alipayConfig.setRootCertPath("D:\\jojo\\bubble\\src\\main\\resources\\cert\\alipayRootCert.crt");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
        String orderNo = String.valueOf(System.currentTimeMillis());
        model.setOutBizNo(orderNo);
        model.setRemark("201905代发");
        model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");
        model.setBizScene("DIRECT_TRANSFER");
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentity("ff7df2@163.com");
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        payeeInfo.setName("郭锐杰");
        model.setPayeeInfo(payeeInfo);
        model.setTransAmount("1.00");
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        model.setOrderTitle("201905代发");
        request.setBizModel(model);
        AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
            System.out.println(response.getCode());
        } else {
            System.out.println("调用失败");
            System.out.println(response.getSubMsg());
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }
}

